import React, { Component } from 'react'
import { Form, Input, Button, message } from 'antd';
import {doLogin} from '@api'
export default class Login extends Component {
  render() {
    return (
      <div>
        <Form
          name="basic"
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 20 }}
          onFinish={this.login}
        >
          <Form.Item
            label="用户名"
            name="userName"
            rules={[{ required: true, message: 'Please input your username!' }]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="密码"
            name="pwd"
            rules={[{ required: true, message: 'Please input your password!' }]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 4, span: 16 }}>
            <Button type="primary" htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    )
  };
  login = (params) => {
    doLogin(params).then(res=> {
      if(res.data.code === 200){
        // 弹出登录成功提示
        message.success(res.data.msg, 2, () => {
          // 结果缓存
          localStorage.setItem('token', res.data.data.token);
          localStorage.setItem('userInfo', JSON.stringify(res.data.data.userInfo));
          this.props.history.replace('/admin')
        })
      }
    })
  }
}
